package com.example.windowdemo.threadpool;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.example.windowdemo.callback.WindowRunnable;

/**
 * @author: njb
 * @date: 2023/8/3 11:31
 * @desc:
 */
public class WindowPool<T> implements WindowRunnable {
    private Context context;
    private boolean isFullScreen;
    private boolean hasTitleBar;
    private int width;
    private int height;
    public T data;
    private static final String TAG = "WindowLog";
    public WindowManager.LayoutParams layoutParams;
    public View contentView;

    public WindowPool(Context context) {
        this.context = context;
        this.layoutParams = new WindowManager.LayoutParams();
        initView();
    }

    private void initView() {

    }

    public void setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
        if (isFullScreen) {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags &= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setTitleBar(boolean hasTitleBar) {
        this.hasTitleBar = hasTitleBar;
        if (hasTitleBar) {
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        layoutParams.width = width;
        layoutParams.height = height;
    }

    public void setPosition(int x, int y) {
        layoutParams.x = x;
        layoutParams.y = y;
    }

    public void setGravity(int gravity) {
        layoutParams.gravity = gravity;
    }

    public void setData(T data){
        this.data = data;
        Log.d(TAG,"===获取到的数据为==="+ data.toString());
    }

    public void addView(View view) {
        this.contentView = view;
    }

    protected boolean autoRegisterEventBus() {
        return false;
    }

    /**
     * 事件分发
     */
    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        return false;
    }

    public void show(WindowThreadPool threadPool) {
        threadPool.execute(this, this);
    }

    public void hide() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.removeView(contentView);
    }

    /**
     * 视图分离
     */
    public void onDetachedFromWindow() {
    }

    @Override
    public void run(WindowPool<?> window) {
        Looper.prepare();
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                windowManager.addView(window.contentView, window.layoutParams);
            }
        });
        Looper.loop();
    }
}
